package com.youhome.controller;

import com.github.pagehelper.PageInfo;
import com.youhome.model.News;
import com.youhome.model.NewsType;
import com.youhome.response.Result;
import com.youhome.response.ResultCode;
import com.youhome.response.ResultPage;
import com.youhome.service.NewsService;
import com.youhome.service.NewsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2018/12/10.
 */
@RestController
@RequestMapping(value = "/news")
@Api(tags = "新闻")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @Autowired
    private NewsTypeService newsTypeService;

    @ApiOperation(value = "新闻列表", notes = "新闻列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页数量", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "type", value = "新闻类别", required = false, paramType = "query")
    })
    @GetMapping(value = "")
    public Object list(Integer pageNumber, Integer pageSize, Integer type) {
        News record = new News();
        if (StringUtils.isEmpty(type)) {
            record.setType(type);
        }
        PageInfo<News> pageInfo = newsService.selectByBean(pageNumber, pageSize, record);
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            return new ResultPage<>(ResultCode.SUCCESS, pageInfo);
        } else {
            return new ResultPage<>(ResultCode.NO_DATA);
        }

    }


    @ApiOperation(value = "新闻详情", notes = "新闻详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long", name = "id", value = "新闻ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{id}")
    public Object details(@PathVariable("id") Long id) {
        if (!StringUtils.isEmpty(id)) {
            News record = newsService.selectByPrimaryKey(id);
            if (record != null) {
                return new Result<>(ResultCode.SUCCESS, record);
            } else {
                return new Result<>(ResultCode.NO_DATA);
            }
        }

        return new Result<>(ResultCode.FAIL);
    }

    @ApiOperation(value = "新闻类别列表", notes = "新闻类别列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "页码", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页数量", required = false, paramType = "query"),
    })
    @GetMapping(value = "/type")
    public Object listType(Integer pageNumber, Integer pageSize) {
        NewsType record = new NewsType();
        record.setStatus(1);
        PageInfo<NewsType> pageInfo = newsTypeService.selectByBean(pageNumber, pageSize, record);
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            return new ResultPage<>(ResultCode.SUCCESS, pageInfo);
        } else {
            return new ResultPage<>(ResultCode.NO_DATA);
        }
    }
}
